package weaver.interfaces.xinri.duss.action;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.xinri.duss.utils.InvokeHelper;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-03-21
 * @Description: 单据流程审批历史创建驳回
 * @ModifyLog:
 **/
public class WeaApprovalHistoryBackAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String remark = requestInfo.getRequestManager().getRemark();
        int nodeid = requestInfo.getRequestManager().getNodeid();
        String fullDate = DateUtil.getFullDate();
        String src = requestInfo.getRequestManager().getSrc();
        String clientIp = requestInfo.getRequestManager().getIp();
        writeLog(">>>>>WeaApprovalHistoryBackAction>>>>>requestid=" + requestid);
        try {
            JSONObject paramjson = new JSONObject();
            rs.executeQuery("select dh,gh,entityType from " + tablename + " where requestid=?", requestid);
            rs.next();
            String dh = Util.null2String(rs.getString("dh"));
            String gh = Util.null2String(rs.getString("gh"));
            String entityType = Util.null2String(rs.getString("entityType"));
            paramjson.put("entityType", entityType);
            paramjson.put("entityOID", "");
            paramjson.put("businessCode", dh);
            paramjson.put("operationType", 1002);
            paramjson.put("operation", 2002);
            paramjson.put("operatorOID", "");
            rs1.executeQuery( "select max(concat(operatedate,' ',operatetime)) datetime,operator,remark from workflow_requestlog \n" +
                    "where requestid=? and nodeid=? and logtype=3 and operator in (select id from hrmresource where status in (0,1,2,3)) " +
                    "group by operator", requestid, nodeid );
            int counts = rs1.getCounts();
            int count = 0;
            String msg = "";
            while (rs1.next()) {
                String datetime = Util.null2String(rs1.getString("datetime"));
                String operator = Util.null2String(rs1.getString("operator"));
                String remark1 = Util.null2String(rs1.getString("remark"));
                paramjson.put("operator", InvokeHelper.getWorkcodeById(operator));
                paramjson.put("operationDetail", InvokeHelper.removeHTMLTags(remark1));
                paramjson.put("createdDate", InvokeHelper.dateFormat(datetime));
                paramjson.put("lastModifiedDate", InvokeHelper.dateFormat(datetime));
                writeLog(">>>>>WeaApprovalHistoryBackAction>>>>>paramjson=" + paramjson);
                String resultStr = InvokeHelper.approvalHistory(paramjson.toJSONString());
                Map<String, Object> dataMap = new HashMap<String, Object>();
                dataMap.put("jkqqcs", paramjson.toJSONString());
                dataMap.put("jkxyfh", resultStr);
                dataMap.put("khdip", clientIp);
                dataMap.put("gllc", requestid);
                dataMap.put("oacz", src);
                dataMap.put("jkdysj", datetime);
                dataMap.put("djh", dh);
                dataMap.put("rqsj", DateUtil.getDateTime());
                dataMap.put("jkmc", "1");
                dataMap.put( "lcid", requestid );
                InvokeHelper.insertApiLog(dataMap, "ApprovalHistory");
                JSONObject resultJson = JSONObject.parseObject(resultStr);
                writeLog(">>>>>WeaApprovalHistoryBackAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("operation completed".equals(message)) {
                    count++;
                } else {
                    msg = message;
                }
            }
            writeLog(">>>>>WeaApprovalHistoryAction>>>>>counts=" + counts+",count="+count);
            if (count == counts) {
                paramjson.put("operator", gh);
                paramjson.put("operationDetail", InvokeHelper.removeHTMLTags(remark));
                paramjson.put("createdDate", InvokeHelper.dateFormat(fullDate));
                paramjson.put("lastModifiedDate", InvokeHelper.dateFormat(fullDate));
                writeLog(">>>>>WeaApprovalHistoryBackAction>>>>>paramjson=" + paramjson);
                String resultStr = InvokeHelper.approvalHistory(paramjson.toJSONString());
                Map<String,Object> dataMap = new HashMap<String, Object>();
                dataMap.put("jkqqcs",paramjson.toJSONString());
                dataMap.put("jkxyfh",resultStr);
                dataMap.put("khdip",clientIp);
                dataMap.put("gllc",requestid);
                dataMap.put("oacz",src);
                dataMap.put("jkdysj",fullDate);
                dataMap.put("djh", dh);
                dataMap.put("rqsj", DateUtil.getDateTime());
                dataMap.put("jkmc", "1");
                dataMap.put( "lcid", requestid );
                InvokeHelper.insertApiLog(dataMap,"ApprovalHistory");
                JSONObject resultJson = JSONObject.parseObject(resultStr);
                writeLog(">>>>>WeaApprovalHistoryBackAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("operation completed".equals(message)) {
                    return Action.SUCCESS;
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent("汇联易接口返回异常:"+message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("汇联易接口返回异常:" + msg);
                return Action.FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
